package display 
{
	import controller.Controller;
	import events.DisplayEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Henrik Riise Hansen
	 */
	
	public class DisplayControls extends MovieClip
	{
		private var control:Controller;
		private var sh:Shell;
		
		// sprites
		private var visibleButton:Sprite;
		private var presetButton:Sprite;
		private var fullScreenButton:Sprite;
		
		private var displayVisibleBtn:DisplayVisibleBtn;
		
		// textfield
		private var displayText:DisplayTextField;
		
		// module list
		private var moduleArray:Array = ['Menu', 'ContentList', 'FeatureList', 'Player','SearchField'];
		
		// panels
		private var visiblePanel:Sprite;
		private var presetPanel:Sprite;
		
		// toggle
		private var selected:Boolean = false;
		
		public function DisplayControls(ctr:Controller,_sh:Shell) 
		{
			control = ctr;
			sh = _sh;
			
			sh.stage.addEventListener(Event.RESIZE, onResize);
			
			createButtons();
			createPanels();
		}
		
		private function createButtons():void
		{
			var visibleBtnText:DisplayTextField = new DisplayTextField('verdana', 0x000000);
			var presetBtnText:DisplayTextField = new DisplayTextField('verdana', 0x000000);
			var fullScreenBtnText:DisplayTextField = new DisplayTextField('verdana',0x000000);
			
			visiblePanel = new Sprite;
			visiblePanel.visible = false;
			
			visibleButton = new Sprite;
			visibleButton.graphics.beginFill(0xffffff, 1);
			visibleButton.graphics.drawRect(0, 0, 110, 20);
			visibleButton.graphics.endFill();
			
			visibleBtnText.label.text = 'Visibility';
			visibleBtnText.label.x = 10;
			visibleBtnText.label.y = 2;
			
			visibleButton.addChild(visibleBtnText);
			visibleButton.addEventListener(MouseEvent.CLICK, onClick);
			
			presetButton = new Sprite;
			presetButton.graphics.beginFill(0xffffff, 1);
			presetButton.graphics.drawRect(0, 0, 110, 20);
			presetButton.graphics.endFill();
			
			presetBtnText.label.text = 'Presets';
			presetBtnText.label.x = 10;
			presetBtnText.label.y = 2;
			
			presetButton.addChild(presetBtnText);
			
			fullScreenButton = new Sprite;
			fullScreenButton.graphics.beginFill(0xffffff, 1);
			fullScreenButton.graphics.drawRect(0, 0, 110, 20);
			fullScreenButton.graphics.endFill();
			fullScreenButton.addEventListener(MouseEvent.CLICK, onFullScreen);
			
			fullScreenBtnText.label.text = 'Fullscreen';
			fullScreenBtnText.label.x = 10;
			fullScreenBtnText.label.y = 2;
			
			fullScreenButton.addChild(fullScreenBtnText);
			
			addChild(fullScreenButton);
			addChild(visibleButton);
			addChild(presetButton);
			
			align();
		}
		
		private function onClick(e:MouseEvent):void
		{
			if (!selected)
			{
				visiblePanel.visible = true;
				selected = true;
			}
			else
			{
				visiblePanel.visible = false;
				selected = false;
			}
		}
		
		private function onFullScreen(e:MouseEvent):void
		{
			if (sh.stage.displayState == StageDisplayState.FULL_SCREEN)
			{
				sh.stage.displayState = StageDisplayState.NORMAL;
			}
			else
			{
				sh.stage.displayState = StageDisplayState.FULL_SCREEN;
			}
		}
		
		private function createPanels():void
		{
			var index:int = 0;
			
			for each(var item:String in moduleArray)
			{
				displayVisibleBtn = new DisplayVisibleBtn;
				displayVisibleBtn.txt.text = item;
				displayVisibleBtn.checkBox.label = item;
				displayVisibleBtn.checkBox.selected = true;
				displayVisibleBtn.checkBox.addEventListener(MouseEvent.CLICK, onCheckBoxClick);
				displayVisibleBtn.y = 25 * index;
				
				visiblePanel.addChild(displayVisibleBtn);
				
				index ++;
			}
			
			addChild(visiblePanel);
		}
		
		private function onCheckBoxClick(e:MouseEvent):void
		{	
			dispatchEvent(new DisplayEvent(DisplayEvent.VISIBILITY_CHANGED, { item:e.currentTarget.label,selected:e.target.selected} ));
		}
		
		private function align():void
		{
			visibleButton.x = 10;
			visibleButton.y = sh.stage.stageHeight - 30;
			
			presetButton.x = 125;
			presetButton.y = sh.stage.stageHeight - 30;
			
			visiblePanel.y = sh.stage.stageHeight - 155;
			visiblePanel.x = 10;
			
			fullScreenButton.y = sh.stage.stageHeight - 30;
			fullScreenButton.x = 240;
			
			fullScreenButton.buttonMode = true;
			visibleButton.buttonMode = true;
			presetButton.buttonMode = true;
		}
		
		private function onResize(e:Event):void
		{
			align();
		}
	}
}